﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VnResource.DataContract.Models;
using VnResource.BusinessLogic;
using Kendo.Mvc.UI;
using Kendo.Mvc.Extensions;

namespace KendoUIMvcApplication3.Controllers
{
    public class HRMController : Controller
    {
        public ActionResult LeaveDay()
        {
            SetViewData();            
            return View();
        }

        #region Load Grid
        public ActionResult EditingPopup_Read([DataSourceRequest] DataSourceRequest request)
        {
            return Json(HRMRepository.Instance.GetLeaveDays().ToDataSourceResult(request));
        } 
        #endregion
        
        #region lay thong tin Profile de load len man hinh modify

        public ActionResult GetInfoToUpDate(string IDStr)
        {
            Guid ID = Guid.Empty;
            Guid.TryParse(IDStr, out ID);
            LeaveDayViewModel leaveday = HRMRepository.Instance.GetLeaveDay(ID);

            string ProfileID = string.Empty;
            string WorkDayFormat = string.Empty;
            string RegisterHour = string.Empty;
            if (leaveday != null)
            {
                if (leaveday.ProfileID != null)
                {
                    ProfileID = leaveday.ProfileID.ToString();
                }
            }

            return Json(new { Profile = leaveday });
        } 
      
        #endregion
        
        #region Insert , UPdate ,Delete
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditingPopup_Create([DataSourceRequest] DataSourceRequest request, LeaveDayViewModel leaveday)
        {
            if (leaveday != null && leaveday.ID != Guid.Empty)
            {
                HRMRepository.Instance.UpdateLeaveDay(leaveday);
            }
            else
            {
                HRMRepository.Instance.InsertLeaveDay(leaveday);
            }
           return RedirectToAction(UIConstants.LeaveDay);
           // return Content(string.Empty);
          //  return Json(new[] { leaveday }.ToDataSourceResult(request, ModelState));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditingPopup_Destroy([DataSourceRequest] DataSourceRequest request, LeaveDayViewModel leaveDay)
        {
            HRMRepository.Instance.DeleteLeaveDay(leaveDay);
            return RedirectToAction(UIConstants.LeaveDay);
        } 
        #endregion
        
        #region Method
        public void SetViewData()
        {

            List<SelectListItem> lstProfileItem = new List<SelectListItem>();
            lstProfileItem = HRMRepository.Instance.GetProfile().Select(p => new SelectListItem
            {
                Text = p.ProfileName,
                Value = p.ID.ToString()
            }).ToList();


            #region leader list
            List<SelectListItem> lstProfileApproveItem = new List<SelectListItem>();
            lstProfileApproveItem = HRMRepository.Instance.GetApproveProfile().Select(p => new SelectListItem
            {
                Text = p.ProfileName,
                Value = p.ID.ToString()
            }).ToList();

            #endregion

            if (lstProfileItem != null)
            {
                Session[UIConstants.Profile] = lstProfileItem;
            }
            if (lstProfileApproveItem != null)
            {

                Session[UIConstants.ApproveProfile] = lstProfileApproveItem;
            }
        }
        #endregion
    }
}
